﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
using System.Net.Mime;
using System.IO;

namespace CADSchool
{
    public partial class Form1 : Form
    {
        CADAxilian spacesEmpty = new CADAxilian();

        public Form1()
        {
            InitializeComponent();
            pnlWarning.Visible = false;

            txtCompleteName.Text = "Digite o nome completo do aluno.";
            txtCompleteName.ForeColor = Color.Silver;
        }

        public bool isFileOpened(string file)
        {
            bool ret = false;

            try
            {
                System.IO.FileStream fs = System.IO.File.OpenWrite(file);
                fs.Close();
            }
            catch (Exception ex)
            {
                ret = true;
            }
            return ret;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            spacesEmpty.MessageWarning(spacesEmpty.EmptySpace(txtCompleteName),
                spacesEmpty.EmptySpace(txtMaskDate), spacesEmpty.EmptySpace(cmbAddress), pnlWarning);

            MailMessage mail = new MailMessage();

            mail.From = new MailAddress("wendel_paz@hotmail.com", "Wendel");
            mail.To.Add("wendelpaz@gmail.com");

            mail.Subject = "Teste de envio de email do aplicativo em c#.";
            mail.Body = "Enviado com sucesso.";
            mail.Priority = MailPriority.High;

            Attachment data = new Attachment("D:\\test.txt", MediaTypeNames.Application.Octet);
            ContentDisposition disp = data.ContentDisposition;
            disp.CreationDate = System.IO.File.GetCreationTime("D:\\test.txt");
            disp.ModificationDate = System.IO.File.GetLastWriteTime("D:\\test.txt");
            disp.ReadDate = System.IO.File.GetLastAccessTime("D:\\test.txt");

            mail.Attachments.Add(data);

            SmtpClient smtp = new SmtpClient("smtp.live.com", 587);
            smtp.EnableSsl = true;

            NetworkCredential cred = new NetworkCredential("wendel_paz@hotmail.com", "#ngc0mp082");
            smtp.Credentials = cred;
            
            try
            {
                smtp.Send(mail);
                MessageBox.Show("Email enviado com sucesso!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro: " + ex.Message + Environment.NewLine + ex.StackTrace);
            }
            
        }

        private void txtCompleteName_Enter(object sender, EventArgs e)
        {
            txtCompleteName.Clear();
            spacesEmpty.StartSpace(txtCompleteName);
        }

        private void txtCompleteName_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCompleteName.Text))
            {
                txtCompleteName.Text = "Digite o nome completo do aluno.";
                txtCompleteName.ForeColor = Color.Silver;
            }
            txtCompleteName.BackColor = Color.Empty;
        }

        private void txtMaskDate_Enter(object sender, EventArgs e)
        {
            spacesEmpty.StartSpace(txtMaskDate);
        }

        private void txtMaskDate_Leave(object sender, EventArgs e)
        {
            txtMaskDate.BackColor = Color.Empty;
        }

        private void cmbAddress_Enter(object sender, EventArgs e)
        {
            spacesEmpty.StartSpace(cmbAddress);
        }

        private void cmbAddress_Leave(object sender, EventArgs e)
        {
            cmbAddress.BackColor = Color.Empty;
        }
    }
}
